package Top4.confirm;

import com.rabbitmq.client.*;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: Administrator
 * @Date: 2019/3/4 13:36
 * @Description: 生产者确认模式
 */
public class NormalProducerConfirm {
    private static final String EXCHANGE_NAME = "exchange_confirm";
    private static final String ROUTING_KEY = "routingkey_confirm";
    private static final String QUEUE_NAME = "queue_confirm";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
//        定义交换器
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
//        定义队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);


        try {
            channel.confirmSelect();

            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, "pulisher confirm test".getBytes());


            if (!channel.waitForConfirms()) {
                System.out.println(" send message failed");
            }else{
                System.out.println(" send message success");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        channel.close();
        connection.close();

    }

}
